$(window).on('load',function () {
    checkIsLogin();
})


var gallery_url =window.login_url.gallery_url+ "gallery2.1/gallery/sgis/auto/login";
var gallery_logouturl =window.login_url.gallery_url+ "gallery2.1/user/logout";

/**
 * 监测是否已经登陆
 * @param url
 * @param callback
 */
var checkIsLogin = function () {
    var url = window.login_url.base_url+"oAuth/isLogin";
    $.ajax({
        type: "GET",
        url: url,
        dataType: "text",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res){
            var re = JSON.parse(res);
            initModule(re.power);
            initUIAlert();
            if(re.status){
                initUserInfo(re);
            }else{
                location.href =SGIS.Util.getBaseUrl() +  "/app/web/base";   //没有登录,直接跳转至首页
            }
        },
        error:function (re) {
            console.log(re);
        }
    });
}
/**
 * 初始化模块
 * @type {{}}
 */
var initModule = function (power) {
    var powerdata = power;
    var js_text = '';
    for(var i in powerdata){
        if(powerdata[i].powerType == 1){
            if(powerdata[i].parid === 0){
                var config_url = powerdata[i].powerValue;
                js_text += "$('.app_"+powerdata[i].powerValue+"').removeClass('disabled');";
                js_text += "$('.app_"+powerdata[i].powerValue+"').click(function(){window.location.href = app_address."+config_url +";});"
            }else{
                for(var j in powerdata){
                    if(powerdata[j].powerid === powerdata[i].parid && powerdata[i].memo==="new" ){//需要新打开页面
                        js_text += "$('.app_"+powerdata[i].powerValue+"').removeClass('disabled');";
                        var config_url = "app_address."+powerdata[j].powerValue+"."+powerdata[i].powerValue;
                        js_text += "$('.app_"+powerdata[i].powerValue+"').click(function(){ window.open("+config_url +",\"_blank\");  });"
                    }
                    if(powerdata[j].powerid === powerdata[i].parid && powerdata[i].memo !="new" ){
                        js_text += "$('.app_"+powerdata[i].powerValue+"').removeClass('disabled');";
                        var config_url = powerdata[j].powerValue+"."+powerdata[i].powerValue;
                        js_text += "$('.app_"+powerdata[i].powerValue+"').click(function(){window.location.href = app_address."+config_url +";});"
                    }
                }
            }
        }
    }
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.text = "var app_address_match = function () {"+js_text+"};$(function () {app_address_match();});";
    $('body').append(script);
    $(".head-nav .drop-down-content a.disabled").unbind("click");
    $("#user .usermenu .item.disabled ").unbind("click");
}

//阻止默认事件函数
function stopDefault(e) {
    if (e && e.preventDefault)
        e.preventDefault();
    else
        window.event.returnValue = false; //兼容IE
}


/**
 * 初始无权限时提醒功能
 */
var initUIAlert = function () {
    $(".head-nav-con .drop-down-content a.disabled").click(function () {
        SGIS.UI.alert("你没有此权限");
    });
    $("#user .item.disabled").click(function () {
        SGIS.UI.alert("你没有此权限");
    });
}
/**
 * 修改页面用户信息
 */
var initUserInfo = function (data) {
    $("#userName").text(data.user.userCaption);
}

var UI = (function () {
    $(function () {
        $('#user .ui.dropdown').dropdown();

        $("#sys-btn-logout").click(function () {
            logOut();  //注销
        });

        $("#sys-btn-gohome").click(function () {
            SGIS.Util.goToLogin();    //回主页
        });

    });
})();

/**
 * 注销登录
 *
 * @constructor
 */
var logOut = function(callback){

    // SGIS.API.get("/oAuth/logout").data().json(function(re){
    //     //图库注销
    //     AutoLoginGalleryHandler.galleryLogout();
    //
    //     location.href =SGIS.Util.getBaseUrl() +  "/app/web/base";   //没有登录,直接跳转至首页
    // });

    var url = window.login_url.base_url+"oAuth/logout";
    $.ajax({
        type: "GET",
        url: url,
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        dataType: "text",
        success: function(re){
            //图库注销
            AutoLoginGalleryHandler.galleryLogout();

            // location.href =SGIS.Util.getBaseUrl() +  "/app/web/base";   //没有登录,直接跳转至首页
            close_wondow();
        },
        error:function (re) {
            console.log(re);
        }
    });
};
/**
 * 关闭当前窗口
 * @type {{}}
 */
var close_wondow = function () {
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Presto") != -1 || userAgent.indexOf("Chrome") !=-1) {
        window.location.replace("about:blank");
    } else {
        window.opener = null;
        window.open('','_self');
        console.log('关闭窗口');
        window.close();
    }
};
/**
 * 自动登录
 * @type {{}}
 */
var AutoLoginGalleryHandler = (function(){
    /**
     * 图库登录
     */
    var galleryLogin = function(userInfo){
        if(userInfo && userInfo != null){
            $.ajax({
                url: gallery_url+"?_method=POST",
                type: "post",
                dataType: "text",
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                data: {
                    userLogin:userInfo.userName,
                    userName:userInfo.userCaption,
                    isAdmin:parseInt(userInfo.sys_role) >= 4
                },
                error: function (e) {
                    console && console.dir(e);
                },
                success: function (re) {
                    console && console.dir(re.message);
                }
            });
        }
    };

    /**
     * 图库注销
     */
    var galleryLogout = function(){
        var url = gallery_logouturl+"?_method=GET";
        jQuery.ajax({
            url: url,
            type: "post",
            dataType: "json",
            data: {},
            error: function (e) {
                console && console.dir(e);
            },
            success: function (re) {
                console && console.dir(re.message);
            }
        });
    };

    return {
        galleryLogin:galleryLogin,
        galleryLogout:galleryLogout

    };
})();



